<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Services\UserService;
use Illuminate\Http\Request;
use App\Models\SMSConfirmationCode;
use App\Models\User;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;

class ForgotPasswordController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Password Reset Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling password reset emails and
    | includes a trait which assists in sending these notifications from
    | your application to your users. Feel free to explore this trait.
    |
    */

    public $subject = '密码重设';

    public $redirectTo = '/login';

    public $userService;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(UserService $userService)
    {
        $this->middleware('guest');
        $this->userService = $userService;
    }


    public function resetPassword(Request $request)
    {
        $this->validate($request, [
            'mobile' => 'required|exists:users',
            'confirmation-code' => 'required|integer',
            'password' => 'required|confirmed|min:6|max:100',
            'password_confirmation' => 'required',
        ], [
            'mobile.required' => '手机必填',
            'mobile.regex' => '手机格式有误',
            'confirmation-code.required' => '验证码必填',
            'confirmation-code.integer' => '验证码只能为数字',
            'password.required' => '密码必填',
            'password_confirmation.required' => '确认密码必填',
            'password.min' => '密码最短为6个字符',
            'password.max' => '密码最长为100个字符',
        ], [
            'mobile' => '手机号',
            'confirmation-code' => '验证码',
            'password' => '密码',
            'password_confirmation' => '确认密码',
        ]);

        $mobile = $request->get('mobile');
        $code = SMSConfirmationCode::where(["mobile" => $mobile])->orderBy('expired', 'desc')->first();
        if ($request->get('password') === $request->get('password_confirmation')) {
            if (!$code) {
                return redirect()->back()->withInput()->withErrors('验证码不存在');
            }
            if ($code && $code->checkSMSConfirmationCode($request->get('confirmation-code'), $code->code, $code->expired)) {
                $password = $request->get('password');
                    $user = User::query()->where('mobile', $mobile)->first();
                    if ($user) {
                      $user->password = bcrypt($password);
                      $user->save();
                    } else {
                        return redirect()->back()->withInput()->withErrors('该用户不存在');
                    }

                return redirect()->to('/login')->with('success', '密码修改成功');
            } else {
                return redirect()->back()->withInput()->withErrors('验证码错误');
            }
        } else {
            return redirect()->back()->withInput()->withErrors('确认密码必须与密码一样');
        }
    }
}
